package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/brand")
@CrossOrigin
public class BrandController {
    @Autowired
    private DtsBrandService dtsBrandService;

    @Autowired
    private DtsAdminService dtsAdminService;

    @Autowired
    private DtsCategoryService dtsCategoryService;
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button ="查找")
    @RequiresPermissions("admin:brand:list")
    @GetMapping("/list")
    public Object brandList(@RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            @RequestParam(defaultValue = "add_time") String sort,
                            @RequestParam(defaultValue = "desc") String order){

        IPage<DtsBrand> iPage = dtsBrandService.findPage(page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsBrand> records = iPage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);
        return ResponseUtil.ok(map);
    }

    @GetMapping("/catAndAdmin")
    public Object catAndAdmin(){

        List<CatVo> adminList = dtsAdminService.findName();

        List<CatVo> categoryList =dtsCategoryService.findName();

        for (CatVo catVo : categoryList) {
            catVo.setChildren(dtsCategoryService.findName2(catVo.getValue())) ;
        }


        Map map = new HashMap();
        map.put("adminList",adminList);
        map.put("categoryList",categoryList);

        return ResponseUtil.ok(map);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button ="编辑")
    @RequiresPermissions("admin:brand:update")
    @PostMapping("/update")
    public Object updateBrand(@RequestBody DtsBrand dtsBrand){

        dtsBrandService.update(dtsBrand);

        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button ="删除")
    @RequiresPermissions("admin:brand:delete")
    @PostMapping("/delete")
    public Object deleteBrand(@RequestBody DtsBrand dtsBrand){

        dtsBrandService.delete(dtsBrand);

        return ResponseUtil.ok();
    }



}
